﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using Umbraco.Ecommerce.BusinessLogic;

namespace Umbraco.Ecommerce.WebShop.UserControls
{
    public partial class ErrorPage : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
            //ConfirmOrder();
        }

        private void BindData()
        {
            try
            {
                int orderid = int.Parse(Request.QueryString["orderid"]);
                System.DateTime time= DateTime.Parse(Request.QueryString["time"]);
                Order o = new BusinessLogic.Order(orderid);
                

                //if (o.SessionId == Session.SessionID)
                if(o.OrderTimeStamp == time)
                
                {
                            
                    BusinessLogic.CustomerInfo custInfo = new Umbraco.Ecommerce.BusinessLogic.CustomerInfo(o.CustomerInformation);
                                       
                    lttFirstname.Text = custInfo.GetValue("firstname");
                    lttLastname.Text = custInfo.GetValue("lastname");
                    lttEmail.Text = custInfo.GetValue("email");
                    lttStreetname.Text = custInfo.GetValue("streetname");
                    lttPostalcode.Text = custInfo.GetValue("postalcode");
                    lttCity.Text = custInfo.GetValue("city");
                    lttTelefon.Text = custInfo.GetValue("telefon");
                    lttCountry.Text = custInfo.GetValue("country");
                    lttCountryCode.Text = custInfo.GetValue("countryCode");

                    DataTable dt = new DataTable();
                    //dt.Columns.Add("ProduktId");
                    dt.Columns.Add("Navn");
                    dt.Columns.Add("Antal");
                    dt.Columns.Add("Pris");
                    dt.Columns.Add("Subtotal");



                    foreach (BusinessLogic.OrderLine ol in o.OrderLines)
                    {
                        DataRow dr = dt.NewRow();
                        //dr["ProduktId"] = ol.ProductId; //ci1.ProductId;
                        dr["Navn"] = ol.ProductName;
                        dr["Antal"] = ol.Quantity;
                        dr["Pris"] = ol.Price.ToString("c", BusinessLogic.Store.NumberFormatInfo).ToLower().Replace("$", "DKK");
                        dr["Subtotal"] = ((decimal)(ol.Quantity * ol.Price)).ToString("c", BusinessLogic.Store.NumberFormatInfo).ToLower().Replace("$", "DKK");
                        dt.Rows.Add(dr);
                    }

                    basketGrid.DataSource = dt;
                    basketGrid.DataBind();

                    decimal totalprice = o.TotalIncVAT;

                    if (BusinessLogic.Store.VatIncludedInPrice)
                    {
                        decimal d = 1 + (decimal.Parse(BusinessLogic.Store.vat.ToString()) / 100);
                        decimal priceExVat = (totalprice / d);
                        lttTotal.Text = (totalprice).ToString("c", BusinessLogic.Store.NumberFormatInfo).ToLower().Replace("$", "DKK");
                        lttTotalExVAT.Text = (priceExVat).ToString("c", BusinessLogic.Store.NumberFormatInfo).ToLower().Replace("$", "DKK");
                        lttVat.Text = (totalprice - priceExVat).ToString("c", BusinessLogic.Store.NumberFormatInfo).ToLower().Replace("$", "DKK");
                    }
                    else
                    {
                        lttTotal.Text = (totalprice * (1 + (decimal.Parse(BusinessLogic.Store.vat.ToString())) / 100)).ToString("c", BusinessLogic.Store.NumberFormatInfo).ToLower().Replace("kr", "DKK");
                        lttTotalExVAT.Text = (totalprice).ToString("c", BusinessLogic.Store.NumberFormatInfo).ToLower().Replace("$", "DKK");
                        lttVat.Text = (totalprice * (1 + (decimal.Parse(BusinessLogic.Store.vat.ToString())) / 100) - totalprice).ToString("c", BusinessLogic.Store.NumberFormatInfo).Replace("kr", "DKK");
                    }
                    /*
                    lttTotal.Text = o.Total.ToString("c", BusinessLogic.Store.NumberFormatInfo).ToLower().Replace("$", "DKK");
                    lttPaymentMethod.Text = new umbraco.cms.businesslogic.Dictionary.DictionaryItem(o.PaymentMethod.Name).Value();
                    pnlError.Visible = false;
                    */


                }
                
                else
                {
                    // Handle url hacking
                    pnlInformation.Visible = false;
                    //pnlError.Visible = true;
                }
                 

            }
            catch { }



        }

    }
}